<?php



namespace think;

use think\exception\HttpResponseException;

return [
    // 控制器开始前，进行权限检查
    'action_begin' => function () {

        $request = app('request');
        list($module, $controller, $action) = [$request->module(), $request->controller(), $request->action()];
        if($module == 'admin'){
            $node = strtolower("{$module}/{$controller}/{$action}");
            $info = Db::name('SystemNode')->cache(true, 30)->where(['node' => $node])->find();
            $access = ['is_menu' => intval(!empty($info['is_menu'])), 'is_auth' => intval(!empty($info['is_auth'])), 'is_login' => empty($info['is_auth']) ? intval(!empty($info['is_login'])) : 1];

            // 登录状态检查
            if (empty($access['is_login']) && empty(session('user'))) {
                if(!in_array(strtolower($module.'/'.$controller.'/'.$action), ['admin/login/index'])){
                    $msg = ['code' => 0, 'msg' => '抱歉，您还没有登录获取访问权限！', 'url' => url('@admin/login/index')];
                    throw new HttpResponseException($request->isAjax() ? json($msg) : redirect($msg['url']));
                }
            }
            // 访问权限检查
            if (!empty($access['is_auth']) && !auth($node)) {
                throw new HttpResponseException(json(['code' => 0, 'msg' => '抱歉，您没有访问权限！']));
            }
        }

        // 模板常量声明
        app('view')->init(config('template.'))->assign(['classuri' => "{$module}/{$controller}"]);
    },
];
